using System;
using System.Collections.Generic;
using System.Text;

namespace BuildingStructuralElements
{
    [MVCFramework .ModelContainment (true )]
    [Serializable]
    public class Bracing:StructuralElement ,BuildingBasicDomain .IGeometry 
    {
        public static Bracing  CreateBracing (BuildingBasicDomain .IArea _area)
        {
            if (_area == null || _area.Polygon.Count == 0) return null ;
            Bracing bracing = new Bracing();
            bracing.bracingBeams = new MVCFramework.DomainCollection<BeamElement>(false);
            bracing.bracingBeams.Add (BeamElement.CreateElement( NodeLine.Create (_area.Polygon[0], _area.Polygon[2])));
            bracing.bracingBeams.Add ( BeamElement.CreateElement( NodeLine.Create (_area.Polygon[1], _area.Polygon[3])));
            return bracing;
        }

        private IList<BeamElement> bracingBeams;

        [MVCFramework .ModelContainment]
        public IList<BeamElement> BracingBeams
        {
            get { return bracingBeams; }
            set { bracingBeams = value; }
        }

        public override bool Move(CommonTypes.Vector v)
        {
            foreach (BeamElement beam in BracingBeams)
                beam.Move(v);
            return true;
        }

        public override bool Move(double dx, double dy, double dz)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #region "Operator Implementation "

        public static bool operator ==(Bracing Bracing1, Bracing Bracing2)
        {
            object o1 = Bracing1;
            object o2 = Bracing2;

            if (o1 == null && o2 == null) return true;
            if (o1 == null) return false;
            if (o2 == null) return false;

            if (HasSameBeams(Bracing1, Bracing2))
                return true;
            else
                return false;

        }

        private static bool HasSameBeams(Bracing Bracing1, Bracing Bracing2)
        {

            foreach (BeamElement element in Bracing1.bracingBeams)
                foreach (BeamElement element2 in Bracing2.bracingBeams )
                    if (element == element2)
                        return true;

            return false;


        }

        public static bool operator !=(Bracing line1, Bracing line2)
        {
            return !(line1 == line2);
        }

        public override bool Equals(object obj)
        {
            if (obj is Bracing)
                return (bool)(this == (Bracing)obj);
            else return false;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        #endregion

        #region IGeometry Members

        public BuildingBasicDomain.IPoint GetMiddle()
        {
            BuildingBasicDomain.IPoint point1 = BracingBeams[0].GetMiddle ();
            BuildingBasicDomain.IPoint point2 = BracingBeams[1].GetMiddle ();
            BuildingBasicDomain.ILine line = new BuildingBasicDomain.Line(point1, point2);

            return line.GetMiddle();
        }

        #endregion
    }

    [MVCFramework .ModelContainment (true)]
    public class BracingBeam : BeamElement
    {

    }

}
